package com.common.base;

import com.common.constants.Constants;
import com.common.entity.BaseEntity;
import com.common.entity.department.DepartmentEntity;
import com.common.entity.system.DictionaryEntity;
import com.common.repository.departmentjob.DepartmentRepository;
import com.common.repository.departmentjob.JobRepository;
import com.common.service.system.DictionaryService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author johnny
 * @create 2020-03-16 下午3:48
 **/
@Component
public class DictionaryAware {


    public static Map<String, String> dictionaryMap = new HashMap<>();

    public static Map<String, String> CHECK_RANK_MAP = new HashMap<>();
    public static Map<String, String> CHECK_ORDER_MAP = new HashMap<>();
    public static Map<String, String> CHECK_CIVIL_MAP = new HashMap<>();

    public static Map<String, Integer> CHART_RANK_LIMIT = new HashMap<>();

    public static Map<Long, String> JOB_MAP = new HashMap<>();

    //------------案件相关 start----------------
    /**
     * 案件类型
     */
    public static Set<String> CASE_TYPE_SET = new HashSet<>();
    /**
     * 承办单元
     */
    public static Set<String> CASE_UNIT_SET = new HashSet<>();
    /**
     * 办案状态
     */
    public static Set<String> CASE_STATUS_SET = new HashSet<>();

    /**
     * 当前阶段
     */
    public static Set<String> CASE_CURRENT_STAGE_SET = new HashSet<>();


    public static Map<String, DictionaryEntity> CASE_INDEX_MAPPING_MAP = new LinkedHashMap<>();

    public static Set<String> CASE_INDEX_JUDICIAL_TYPE = new HashSet<>();


    //------------案件相关 end----------------


    public static Map<Long, DepartmentEntity> DEPARTMENT_MAP = new HashMap<>();

    public static Set<String> ALL_FILL_FORM_ROLES = new HashSet<>();
    public static Set<String> ALL_RISK_QUALITY_FLOW_FORM_ROLES = new HashSet<>();


    private final DictionaryService dictionaryService;


    private final DepartmentRepository departmentRepository;

    private final JobRepository jobRepository;

    public DictionaryAware(DictionaryService dictionaryService, DepartmentRepository departmentRepository, JobRepository jobRepository) {
        this.dictionaryService = dictionaryService;
        this.departmentRepository = departmentRepository;
        this.jobRepository = jobRepository;
    }


    @PostConstruct
    public void init() {
        //查询 语言字典， 节目 一级 二级分类 字典
//        String[] typeArr = {DictionaryTypeEnum.LANGUAGE_ID.getCode(), DictionaryTypeEnum.PROGRAM_TYPE.getCode(), DictionaryTypeEnum.PROGRAM_CLASS.getCode(), DictionaryTypeEnum.TAG.getCode()};
//        List<DictionaryEntity> dictionaryEntities = dictionaryService.findByType(typeArr);
//        dictionaryEntities.stream().forEach(dictionaryEntity -> {
//            dictionaryMap.put(dictionaryEntity.getName(), dictionaryEntity.getValue());
//        });

        List<DictionaryEntity> checkRankEntity = dictionaryService.findByType(new String[]{Constants.CHECK_RANK});
        checkRankEntity.forEach(dictionaryEntity -> {
            CHECK_RANK_MAP.put(dictionaryEntity.getName(), dictionaryEntity.getValue());
        });

        List<DictionaryEntity> checkOrderEntity = dictionaryService.findByType(new String[]{Constants.CHECK_ORDER});
        checkOrderEntity.forEach(dictionaryEntity -> {
            CHECK_ORDER_MAP.put(dictionaryEntity.getName(), dictionaryEntity.getValue());
        });

        List<DictionaryEntity> checkCivilEntity = dictionaryService.findByType(new String[]{Constants.CHECK_CIVIL});
        checkCivilEntity.forEach(dictionaryEntity -> {
            CHECK_CIVIL_MAP.put(dictionaryEntity.getName(), dictionaryEntity.getValue());
        });

        List<DictionaryEntity> chartRankLimitEntity = dictionaryService.findByType(new String[]{Constants.CHART_RANK_LIMIT});

        chartRankLimitEntity.forEach(dictionaryEntity -> {
            CHECK_CIVIL_MAP.put(Constants.CHART_RANK_LIMIT, dictionaryEntity.getValue());
        });

        //案件类型
        List<DictionaryEntity> caseTypeEntity = dictionaryService.findByType(new String[]{Constants.CASE_TYPE});
        caseTypeEntity.forEach(dictionaryEntity -> CASE_TYPE_SET.add(dictionaryEntity.getName()));

        //承办单位
        List<DictionaryEntity> caseUnitEntity = dictionaryService.findByType(new String[]{Constants.CASE_UNIT});
        caseUnitEntity.forEach(dictionaryEntity -> CASE_UNIT_SET.add(dictionaryEntity.getName()));

        //办案状态
        List<DictionaryEntity> caseStatusEntity = dictionaryService.findByType(new String[]{Constants.CASE_STATUS});
        caseStatusEntity.forEach(dictionaryEntity -> CASE_STATUS_SET.add(dictionaryEntity.getName()));

        //当前阶段
        List<DictionaryEntity> caseCurrentStageEntity = dictionaryService.findByType(new String[]{Constants.CASE_CURRENT_STAGE});
        caseCurrentStageEntity.forEach(dictionaryEntity -> CASE_CURRENT_STAGE_SET.add(dictionaryEntity.getName()));

        //部门
        DEPARTMENT_MAP = departmentRepository.findAll().stream().collect(Collectors.toMap(BaseEntity::getId, v -> v));

        //查看 填表项所有的角色
        dictionaryService.findByType(new String[]{Constants.ALL_FILL_FORM_ROLES})
                .forEach(dictionaryEntity -> ALL_FILL_FORM_ROLES.add(dictionaryEntity.getName()));
        //查看 风险评估 角色
        dictionaryService.findByType(new String[]{Constants.ALL_RISK_QUALITY_FLOW_FORM_ROLES})
                .forEach(dictionaryEntity -> ALL_RISK_QUALITY_FLOW_FORM_ROLES.add(dictionaryEntity.getName()));

        //岗位
        jobRepository.findAll().forEach(jobEntity -> JOB_MAP.put(jobEntity.getId(), jobEntity.getJobName()));

        //首页
        List<DictionaryEntity> caseTypeMappingEntity = dictionaryService.findByTypeAndOrder(Constants.CASE_INDEX_MAPPING_MAP);
        caseTypeMappingEntity.forEach(dictionaryEntity ->
                CASE_INDEX_MAPPING_MAP.put(dictionaryEntity.getName(), dictionaryEntity));

        // 首页检察官案件类型
        List<DictionaryEntity> caseIndexJudicialTypeEntity = dictionaryService.findByType(new String[]{Constants.CASE_INDEX_JUDICIAL_TYPE});
        caseIndexJudicialTypeEntity.forEach(dictionaryEntity -> CASE_INDEX_JUDICIAL_TYPE.add(dictionaryEntity.getName()));
    }

    public static void refresh(DictionaryService dictionaryService) {


    }
}
